package com.attendance.service.impl;

import com.attendance.common.CommonPage;
import com.attendance.common.CommonResult;
import com.attendance.mapper.AttendanceMapper;
import com.attendance.mapper.CourseMapper;
import com.attendance.mapper.StudentMapper;
import com.attendance.pojo.Attendance;
import com.attendance.pojo.Course;
import com.attendance.pojo.Pages;
import com.attendance.pojo.Student;
import com.attendance.util.BeanCopyUtils;
import com.attendance.vo.AttendanceVO;
import com.attendance.vo.CourseVO;
import com.attendance.vo.EntranceVO;
import com.attendance.service.AttendanceService;
import com.attendance.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    AttendanceMapper attendanceMapper;
    /**
     * 查询门禁打卡记录
     * @param paramMap 参数集合
     * @return
     */
    @Override
    public CommonResult findStudentEntranceByParamMap(HashMap<String, Object> paramMap, Pages pages) {



        //设置查询条件
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        EntranceVO entranceVO = (EntranceVO) paramMap.get("entranceVO");
        //设置开始时间
        if(StringUtils.hasText((String) paramMap.get("dateBegin"))) wrapper.apply("date_format (attTime,'%Y-%m-%d') >= date_format('" + DateUtil.stringToDate((String) paramMap.get("dateBegin")) + "','%Y-%m-%d')");
        //设置结束时间
        if(StringUtils.hasText((String) paramMap.get("dateEnd"))) wrapper.apply("date_format (attTime,'%Y-%m-%d') <= date_format('" + DateUtil.stringToDate((String) paramMap.get("dateEnd")) + "','%Y-%m-%d')");
        //设置查询学号
        wrapper.like(StringUtils.hasText(entranceVO.getNumber()),"stu.stuNum",entranceVO.getNumber())
        //设置查询姓名
                .like(StringUtils.hasText(entranceVO.getName()),"stu.stuName",entranceVO.getName());

        PageHelper.startPage(pages.getPage(), pages.getNum());
        List<EntranceVO> list = attendanceMapper.findStudentEntranceByWrapper(wrapper);
        CommonPage<EntranceVO> entranceCommonPage = CommonPage.restPage(list);
        //FIXME 返回处理
        if(list.size()==0) return CommonResult.success("未查询到数据");
        return  CommonResult.success(entranceCommonPage);

    }

    @Override
    public CommonResult findStudentAttendanceByParamMap(HashMap<String, Object> paramMap,Pages pages) {


        //设置查询条件
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        AttendanceVO attendanceVO = (AttendanceVO) paramMap.get("attendanceVO");
        //设置开始时间
        if(StringUtils.hasText((String) paramMap.get("dateBegin"))) wrapper.apply("date_format (attTime,'%Y-%m-%d') >= date_format('" + DateUtil.stringToDate((String) paramMap.get("dateBegin")) + "','%Y-%m-%d')");
        //设置结束时间
        if(StringUtils.hasText((String) paramMap.get("dateEnd"))) wrapper.apply("date_format (attTime,'%Y-%m-%d') <= date_format('" + DateUtil.stringToDate((String) paramMap.get("dateEnd")) + "','%Y-%m-%d')");
        //设置查询学号
        wrapper.like(StringUtils.hasText(attendanceVO.getStuNumber()),"stu.stuNum",attendanceVO.getStuNumber())
        //设置查询姓名
                .like(StringUtils.hasText(attendanceVO.getStuName()),"stu.stuName",attendanceVO.getStuName())
        //设置查询院系
                .eq(StringUtils.hasText(attendanceVO.getApartment()),"c.apartment",attendanceVO.getApartment())
        //设置查询专业
                .eq(StringUtils.hasText(attendanceVO.getMajor()),"c.major",attendanceVO.getMajor())
        //设置查询班级
                .eq(StringUtils.hasText(attendanceVO.getClassCode()),"c.classCode",attendanceVO.getClassCode())
        //设置查询项目名称(课程)
                .eq(StringUtils.hasText(attendanceVO.getCourse()),"co1.couName",attendanceVO.getCourse())
        //设置查询考勤情况
                .eq(attendanceVO.getCondition()!=null,"att.attCondition",attendanceVO.getCondition())
                .orderByAsc("attTime");

        //查询返回并封装分页信息
        PageHelper.startPage(pages.getPage(), pages.getNum());
        List<AttendanceVO> list = attendanceMapper.findStudentAttendanceByWrapper(wrapper);
        CommonPage<AttendanceVO> entranceCommonPage = CommonPage.restPage(list);
        if(list.size()==0) return CommonResult.success("未查询到数据");
        return CommonResult.success(entranceCommonPage);
    }
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    CourseMapper courseMapper;
    @Override
    @Transactional
    public void uploadAttendanceRecord(List<AttendanceVO> attendanceVOs) {
        //TODO
        attendanceVOs.stream()
                .map(new Function<AttendanceVO, Attendance>() {
                    @Override
                    public Attendance apply(AttendanceVO attendanceVO) {
                        Attendance attendance = BeanCopyUtils.copyBean(attendanceVO, Attendance.class);
                        Student student = new Student();
                        student.setStuName(attendanceVO.getStuName());
                        student.setStuNum(attendanceVO.getStuNumber());
                        attendance.setStuId(studentMapper.selectStudent(student).get(0).getStuId());
                        CourseVO courseVO = new CourseVO();
                        courseVO.setCouName(attendanceVO.getCourse());
                        courseVO.setTeaName(attendanceVO.getDutyTeacher());
                        attendance.setCouId(courseMapper.selectCourse(courseVO).get(0).getCouId());
                        return attendance;
                    }
                })
                .forEach(attendance -> attendanceMapper.insert(attendance));

    }
}
